package com.umer.pinban.adapter.customer;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.common.ui.recycleview.adapter.RecyclerviewBasicAdapter;
import com.common.ui.recycleview.base.ViewHolder;
import com.common.ui.recycleview.listener.DataStateListener;
import com.common.ui.recycleview.listener.OptListener;
import com.umer.pinban.R;
import com.umer.pinban.bean.customer.CustomerBean;
import com.umer.pinban.customer.view.RoundTextView;
import java.util.List;

public class CustomerManagerAdapter extends RecyclerviewBasicAdapter<CustomerBean> {

    private OptListener optListener;

    public CustomerManagerAdapter(Context context, List<CustomerBean> datas, int layoutId,
                                  OptListener optListener, DataStateListener dataStateListener) {
        super(context, datas, layoutId,dataStateListener);
        this.optListener = optListener;
    }

    @Override
    public void convert(ViewHolder holder, final CustomerBean customer, final int position) {

        RoundTextView tv1 = holder.getView(R.id.tv1);
        RoundTextView tv2 = holder.getView(R.id.tv2);
        RoundTextView tv3 = holder.getView(R.id.tv3);
        RoundTextView tv4 = holder.getView(R.id.tv4);
        TextView tv_head = holder.getView(R.id.tv_head);
        TextView tv_name = holder.getView(R.id.tv_name);
        TextView tv_phone = holder.getView(R.id.tv_phone);
        ImageView iv_birthday_icon = holder.getView(R.id.iv_birthday_icon);
        ImageView iv_message_icon = holder.getView(R.id.iv_message_icon);
        String name = customer.getName();
        if(name!=null && !name.equals("") && !name.equals(" ") && !name.equals("null")){
            tv_head.setText(name);
            String firstName = name.substring(0,1);
            tv_head.setText(firstName);
            if(!isChinese(firstName)){
                tv_head.setText(firstName.toUpperCase());
            }
            tv_name.setText(name);
        }else {
            tv_name.setText("");
        }
        String phone = customer.getPhone();
        if(phone!=null && !phone.equals("") && !phone.equals(" ") && !phone.equals("null")){
            tv_phone.setText(phone.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2"));
        }else {
            tv_phone.setText("");
        }
        int makeStatus = customer.getMakeStatus();
        //明日没有预约
        if(makeStatus == 0){
            tv_head.setBackgroundResource(R.drawable.shape9);
        }
        //明日有约
        else {
            tv_head.setBackgroundResource(R.drawable.shape10);
        }

        //生日
        String birthdayColor = customer.getBirthdayColor();
        if(birthdayColor!=null && !birthdayColor.equals("") && !birthdayColor.equals(" ")
                && !birthdayColor.equals("null")){
            iv_birthday_icon.setBackgroundColor(Color.parseColor(birthdayColor));
        }else {
            iv_birthday_icon.setBackgroundColor(Color.parseColor("#AAAAAA"));
        }

        //消息提醒 0没有提醒
        int remindStatus = customer.getRemindStatus();
        if(remindStatus == 0){
            iv_message_icon.setBackgroundColor(Color.parseColor("#AAAAAA"));
        }else {
            String remindColor = customer.getRemindColor();
            if(remindColor!=null && !remindColor.equals("") && !remindColor.equals(" ")
                    && !remindColor.equals("null")){
                iv_message_icon.setBackgroundColor(Color.parseColor(remindColor));
            }else {
                iv_message_icon.setBackgroundColor(Color.parseColor("#AAAAAA"));
            }
        }

        //1 所属优美师
        String personnelName = customer.getPersonnelName();
        if(personnelName!=null && !personnelName.equals("") && !personnelName.equals(" ")
                && !personnelName.equals("null")){
            tv1.setBackgroungColor1(Color.parseColor("#FFC0CB"));
            tv1.setVisibility(View.VISIBLE);
            tv1.setText(personnelName+"("+"美容师"+")");
        }else {
            tv1.setText("");
            tv1.setVisibility(View.GONE);
        }

        //2 多少天未进店
        String expenseSevenDayLabel = customer.getExpenseSevenDayLabel();
        if(expenseSevenDayLabel!=null && !expenseSevenDayLabel.equals("") && !expenseSevenDayLabel.equals(" ")
                && !expenseSevenDayLabel.equals("null")){
            tv2.setVisibility(View.VISIBLE);
            tv2.setText(expenseSevenDayLabel);
            String expenseSevenDayColor = customer.getExpenseSevenDayColor();
            if(expenseSevenDayColor!=null && !expenseSevenDayColor.equals("") && !expenseSevenDayColor.equals(" ") && !expenseSevenDayColor.equals("null")){
                tv2.setBackgroungColor1(Color.parseColor(expenseSevenDayColor));
            }else {
                tv2.setBackgroungColor1(Color.parseColor("#C2242B"));
            }
        }else {
            tv2.setVisibility(View.GONE);
            tv2.setText("");
            tv2.setBackgroungColor1(Color.parseColor("#FFFFFF"));
        }

        //3 几天前消费
        String payThirtyDayLabel = customer.getPayThirtyDayLabel();
        if(payThirtyDayLabel!=null && !payThirtyDayLabel.equals("") && !payThirtyDayLabel.equals(" ") && !payThirtyDayLabel.equals("null")){
            String payThirtyDayColor = customer.getPayThirtyDayColor();
            tv3.setVisibility(View.VISIBLE);
            tv3.setText(payThirtyDayLabel);
            if(payThirtyDayColor!=null && !payThirtyDayColor.equals("") && !payThirtyDayColor.equals(" ") && !payThirtyDayColor.equals("null")){
                tv3.setBackgroungColor1(Color.parseColor(payThirtyDayColor));
            }else {
                tv3.setBackgroungColor1(Color.parseColor("#C2242B"));
            }
        }else {
            tv3.setVisibility(View.GONE);
            tv3.setText("");
            tv3.setBackgroungColor1(Color.parseColor("#FFFFFF"));
        }

        //4 经期
        String sex = customer.getSex();
        if(sex!=null && !sex.equals("") && !sex.equals(" ") && !sex.equals("null")){
            if(sex.equals("男")){
                tv4.setVisibility(View.GONE);
                tv4.setText("男");
                tv4.setBackgroungColor1(Color.parseColor("#888787"));
            }else {
                if(customer.getMenstrualLabel()!=null &&
                        !customer.getMenstrualLabel().equals("") && !customer.getMenstrualLabel().equals(" ")
                        && !customer.getMenstrualLabel().equals("null")){
                    tv4.setVisibility(View.VISIBLE);
                    tv4.setText(customer.getMenstrualLabel());
                    String menstrualColor = customer.getMenstrualColor();
                    if(menstrualColor !=null &&!menstrualColor.equals("") && !menstrualColor.equals(" ") && !menstrualColor.equals("null")){
                        tv4.setBackgroungColor1(Color.parseColor(menstrualColor));
                    }
                }else {
                    tv4.setVisibility(View.GONE);
                    tv4.setText("未设置经期");
                    tv4.setBackgroungColor1(Color.parseColor("#888787"));
                }
            }
        }else {
            tv4.setVisibility(View.GONE);
            tv4.setText("男");
            tv4.setBackgroungColor1(Color.parseColor("#888787"));
        }

        tv4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                optListener.onOptClick(v,position);
            }
        });
    }

    // 判断一个字符是否是中文
    public boolean isChinese(String str) {
        for (int i = 0; i < str.length(); i++) {
            String bb = str.substring(i, i + 1);
            // 生成一个Pattern,同时编译一个正则表达式,其中的u4E00("一"的unicode编码)-\u9FA5("龥"的unicode编码)
            boolean cc = java.util.regex.Pattern.matches("[\u4E00-\u9FA5]", bb);
            if (cc == false) {
                return cc;
            }
        }
        return true;
    }
}
